package com.hnevc.acountms.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hnevc.acountms.R;
import com.hnevc.acountms.dao.PwdDao;
import com.hnevc.acountms.model.Tb_pwd;

public class SystemSetActivity extends AppCompatActivity {

    EditText mEtOldPassword;
    EditText mEtNewPassword;
    EditText mEtNewPasswordAgain;
    Button mBtnUpdate;
    Button mBtnCancel;

    PwdDao pwdDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_set);

        initView();
        initEvent();
    }

    private void initEvent() {
        pwdDao = new PwdDao();
        mBtnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tb_pwd pwd = pwdDao.find(1);
                if (pwd.getPassword().equals(mEtOldPassword.getText().toString())) {
                    if (mEtNewPassword.getText().toString()
                            .equals(mEtNewPasswordAgain.getText().toString())) {
                        pwd.setPassword(mEtNewPassword.getText().toString());
                        pwdDao.update(pwd);
                        Toast.makeText(SystemSetActivity.this, "系统密码设置成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(SystemSetActivity.this, "系统密码设置失败", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(SystemSetActivity.this, "系统密码设置失败", Toast.LENGTH_SHORT).show();
                }
            }

        });
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEtOldPassword.setText("");
                mEtNewPasswordAgain.setText("");
                mEtNewPassword.setText("");
            }
        });
    }

    private void initView() {
        mEtOldPassword = (EditText) findViewById(R.id.id_tv_old_password);
        mEtNewPassword = (EditText) findViewById(R.id.id_tv_new_password);
        mEtNewPasswordAgain = (EditText) findViewById(R.id.id_tv_new_password_again);
        mBtnUpdate = (Button) findViewById(R.id.id_btn_update);
        mBtnCancel = (Button) findViewById(R.id.id_btn_cancel);
    }

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, SystemSetActivity.class);
        context.startActivity(intent);
    }
}
